package org.link.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.link.bean.User;
import org.link.service.BoardService;
import org.link.service.UserService;

/**
 * 
 * project_name:forumweb
 * package_name:org.link.web
 * file_name:IndexServlet.java
 * @author Link
 * create_date:2015年10月22日 下午2:11:45
 * modified_date:
 * description: 管理员初次登录servlet类
 *
 */
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userservice = new UserService();
	private BoardService boardService = new BoardService();
       
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		Cookie[] cookies = request.getCookies();
		String email = null;
		String pass = null;
		User user = new User();
		if(cookies!=null){
			for (Cookie cookie : cookies) {	
				if(cookie.getName().equals("hello1")){
					email = cookie.getValue();
					user.setEmail(email);
				}
				if(cookie.getName().equals("hello2")){
					pass = cookie.getValue();
					user.setPass(pass);
				}
			}
			if((email==null)||(pass==null)){
				request.getRequestDispatcher("manager-index.jsp").forward(request, response);
			}else{
				if(userservice.managerLogin(user)==0){
					session.setAttribute("allBoard", boardService.QueryAllBoard());
					session.setAttribute("allFatherBoard", boardService.queryAllFatherBoard());
					response.sendRedirect("pages/home.jsp");
				}
			}	
		}else{
			response.sendRedirect("manager-index.jsp");
		}
	}
}
